import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User } from 'src/schemas/user.schema';
import { RegisterDto } from '../auth/dto';

@Injectable()
export class UserService {
  constructor(@InjectModel(User.name) private userModel: Model<User>) {}
  async create(user: RegisterDto) {
    return await this.userModel.create(user);
  }

  async findByPhone(phone: string) {
    return await this.userModel.findOne({ phone }).select('+password');
  }

  async findAll() {
    return await this.userModel.find();
  }
  async findById(id: string) {
    return await this.userModel.findById(id);
  }

  async createWechat(openid: string) {
    const user = await this.userModel.find({ openid });
    if (user.length === 0) {
      return await this.userModel.create({ openid });
    }
    return user[0];
  }
}
